//
//  WelcomePavilionViewController.m
//  WelcomePavilion
//
//  Created by wangjun on 10-8-17.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "WelcomePavilionViewController.h"
#import "XmlWelcome.h"
#import "IconDownloader.h"
@implementation WelcomePavilionViewController
@synthesize array,tableView,imageDownloadsInProgress;
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
	if ([self.array count]==0) {
		[NSThread detachNewThreadSelector:@selector(myTaskMethod) toTarget:self withObject:nil];
	}
}
-(void)myTaskMethod
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
	XmlWelcome *parser=[[XmlWelcome alloc]
						initWithContentsOfURL:[NSURL URLWithString:@"http://mp.myvsp.cn/welcomedemos/getpavilionxml.json?area=a&width=80&height=80&digest_length=20"]];
	//设置代理
	[parser setDelegate:parser];
	[parser parse];
	self.array=parser.ones;
	[self.tableView reloadData];
	[parser  release];
	[pool release];

}
- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	self.array=nil;
	self.tableView=nil;
}
- (void)dealloc {
	[self.tableView release];
	[self.array release];
    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
	return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView 
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];
	
	if (cell==nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
									   reuseIdentifier:@"tag"] autorelease];
	}	
	//表格设计
	NSDictionary* one = [array objectAtIndex:indexPath.row];
	cell.textLabel.text = [one objectForKey:@"title"];
	cell.detailTextLabel.text = [one objectForKey:@"content"];
	 [NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
	return cell;
}

- (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    UIImage *image = [self getImageForCellAtIndexPath:indexPath];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
	[image release];
    [pool release];
}
-(UIImage *)getImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
	id path = [[array objectAtIndex:indexPath.row] objectForKey:@"image"];
	NSURL *url = [NSURL URLWithString:path];
	NSData *data = [NSData dataWithContentsOfURL:url];
	UIImage *image = [[UIImage alloc] initWithData:data cache:NO];
	return image;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return @"Hobby Information:";
}
@end
